package online.afeibaili.storebackendframework.controller

import online.afeibaili.storebackendframework.model.ProductEntity
import online.afeibaili.storebackendframework.service.impl.ProductServiceImpl
import online.afeibaili.storebackendframework.util.Response
import online.afeibaili.storebackendframework.util.data
import online.afeibaili.storebackendframework.util.ok
import online.afeibaili.storebackendframework.util.response
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*


/**
 * 公开的控制器
 *
 *@author AfeiBaili
 *@version 2025/10/21 14:08
 */

@RestController
@RequestMapping("/public")
class PublicController {
    @Autowired
    lateinit var service: ProductServiceImpl

    /**
     * ## 根据id返回商品
     *
     * @param id 商品ID
     * @return 商品存在返回商品，否则返回400响应
     */
    @GetMapping("/product/get/id")
    fun getProductById(@RequestParam id: Long): Response {
        val product = service.getById(id)
        if (product != null) {
            return ok(200, product)
        }
        return ok(400, "id is not found")
    }

    /**
     * ## 根据字段值获取商品
     *
     * @param page 第几页
     * @param count 多少个
     * @param type 只能是price值和sales值
     * @param sort 排序方式：asc、desc
     */
    @PostMapping("/product/get/by/type")
    fun getPriceAndSalesPage(
        @RequestParam page: Int,
        @RequestParam count: Int,
        // 只能是price和sales
        @RequestParam type: String,
        // 只能是asc和desc
        @RequestParam sort: String,
    ) = response {
        val entities: List<ProductEntity> = service.getByPage(page, count, type, sort)
        200 data entities
    }

    /**
     * ## 获取热销商品
     * @param page 第几页
     * @param count 数量
     */
    @PostMapping("/product/get/hot")
    fun getHotProduct(
        @RequestParam page: Int,
        @RequestParam count: Int,
    ) = response {
        200 data service.getByHot(page, count)
    }

    /**
     * ## 根据价格的大小获取商品
     * @param page 第几页
     * @param count 数量
     */
    @PostMapping("/product/get/price")
    fun getPriceProduct(
        @RequestParam page: Int,
        @RequestParam count: Int,
    ) = response {
        200 data service.getByPrice(page, count)
    }

    /**
     * ## 根据分页获取产品
     * @param page 第几页
     * @param count 数量
     */
    @PostMapping("/product/get/page")
    fun getProductByPage(
        @RequestParam page: Int,
        @RequestParam count: Int,
    ) = response {
        200 data service.getByPage(page, count)
    }

    /**
     * ## 根据关键词和页面获取产品
     *
     * @param keyWord 关键词
     * @param page 第几页
     * @param count 一页中的数据
     */
    @PostMapping("/product/get/keyword")
    fun getProductByKey(
        @RequestParam keyWord: String,
        @RequestParam page: Int,
        @RequestParam count: Int,
    ) = response {
        200 data service.getKeyWordByNameAndDetail(keyWord, page, count)
    }

    /**
     * ## 获取随机数量的产品
     * @param count 数量
     */
    @PostMapping("/product/get/random")
    fun getProductByRandom(
        @RequestParam count: Int,
    ) = response {
        200 data service.getRandomProduct(count)
    }

    /**
     * ## 获取积分商品
     * @param page 第几页
     * @param count 数量
     */

    @PostMapping("/product/get/points")
    fun getPointsProduct(
        @RequestParam page: Int,
        @RequestParam count: Int,
    ) = response {
        200 data service.getPointsProduct(page, count)
    }
}